package com.ziyun.pop.mall.module.user.web;



import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.service.common.protocol.ResultCode;
import com.service.ucenter.api.VerifyCodeService;
import com.service.ucenter.constant.UcenterResult;
import com.service.ucenter.model.verify.VerifyCodeDto;
import com.utility.JsonUtils;
import com.utility.http.HttpUtils;
import com.ziyun.pop.mall.module.user.entity.SysUserEntity;
import com.ziyun.pop.mall.module.user.service.PopUserAdvanceService;
import com.ziyun.pop.mall.system.config.HttpRequestLocal;


/**
 * security安全中心接口
 * @author husong
 *
 */

@Controller
public class UserSecurityController {
	private static Logger _log = LoggerFactory.getLogger(UserSecurityController.class);
	private static final String MODEL = "/user/popUserSecurity";
	
	@Value("${erp-api}")
	private String erpApi;
		 
	@Autowired
	private VerifyCodeService verifyCodeService;
	
	@Autowired
	private PopUserAdvanceService userAdvanceService;
	
	@Autowired
	private HttpRequestLocal httpRequestLocal;
	

	/**
	 * 进入用户安全中心页面
	 * @return
	 */
	@GetMapping(MODEL+"/index.do")
	public ModelAndView lookUserSecurity(){
		ModelAndView view = new ModelAndView("/user/userSecurity/index.html");
		return view;
	}
	
	/**
	 * 进入预支付密码页面
	 * @return
	 */
	@GetMapping(MODEL+"/setAdvancePass")
	public ModelAndView lookSetAdvancePass(){
		ModelAndView view = new ModelAndView("/user/userSecurity/setAdvancePass.html");
		return view;
	}
	
	/**
	 * 进入修改登录密码页面
	 * @return
	 */
	@GetMapping(MODEL+"/setLoginPass")
	public ModelAndView lookSetLoginPass(){
		ModelAndView view = new ModelAndView("/user/userSecurity/setLoginPass.html");
		return view;
	}
	
	/**
	 * 获取验证码+智能验证
	 * @throws Exception 
	 */
	@PostMapping(MODEL+"/getVerifyCode.json")
	@ResponseBody
	public UcenterResult smartVerify(String verifyCodeData) throws Exception{
		
		VerifyCodeDto verifyCodeDto = JsonUtils.string2Object(verifyCodeData, VerifyCodeDto.class);	
			 return verifyCodeService.getVerifyCodeValidation(verifyCodeDto);
	}
	
	/**
	 * 校验验证码
	 * @throws Exception 
	 */
	@PostMapping(MODEL+"/checkVerifyCode.json")
	@ResponseBody
	public UcenterResult<?> checkVerifyCode(String mobile, String verifyCode){		
			return verifyCodeService.checkVerifyCode(mobile,verifyCode);		
	}
	
	
	/**
	 * 修改预支付密码
	 */
	@PostMapping(MODEL+"/setAdvancePass.json")
	@ResponseBody
	public UcenterResult setAdvancePass(String mobile,String newPassword,String loginName){		
			return userAdvanceService.saveAdvancePass(mobile,newPassword,loginName);
			//return UcenterResult.success();
		
	}	
		
		/**
		 * 修改登陆用户密码
		 */
		@PostMapping(MODEL+"/setLoginPass.json")
		@ResponseBody
		public UcenterResult<?> setLoginPass(String newPassWord){
			// 获取用户userId
		    Object PopUserId = httpRequestLocal.getSessionValue("popUserId");
		    Map<String, Object> map = null;
		    UcenterResult<?> result  = null;
		    if(StringUtils.isEmpty(PopUserId)){
		    	result = UcenterResult.error(ResultCode.ACCOUNT_NOT_EXIST);
		    	return result;
		    }
	    	long userId =  Long.valueOf(String.valueOf(PopUserId)).longValue();	
		    if (userId > 0) { 			    				    
		      SysUserEntity popUserInfo = SysUserEntity.builder().userId(userId).passwordOld(newPassWord).build();	  
              String doPost = HttpUtils.doPost( erpApi + "/port/sys/updateUser", null, JsonUtils.object2String(popUserInfo));
              map = JsonUtils.string2Map(doPost);	              
			}
		    
		    if((Integer) map.get("code") == 0){
		    	result = UcenterResult.success();
				result.setMessage("密码修改成功");
				return result;
          	     
            }
		    result = UcenterResult.error(ResultCode.ACCOUNT_NOT_EXIST);
	    	return result;
            
		    
	}
}
